import java.util.*;

// 已知前序遍历，将其转变成中序遍历输出
class TreeNode{
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val){
        this.val=val;
    }
}

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Test {
    public static int i=0;//i不能放在createTree函数里面，被static修饰只有一份，若反复调用createTree可能i的值不为1

    public static TreeNode createTree(String str){
        TreeNode root=null;
        if(str.charAt(i)!='#'){
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createTree(str);
            root.right=createTree(str);
        }else{
            i++;
        }
        return root;
    }
    public static void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
//        // 注意 hasNext 和 hasNextLine 的区别
////        while (in.hasNextLine()) { // 注意 while 处理多个 case
////
////        }
//        String str=in.nextLine();
//        TreeNode root=createTree(str);
//        inOrder(root);
        System.out.println(1+1*(9));
    }
    public void levelOrder(TreeNode root){
        //1.创建一个队列
        Queue<TreeNode> queue=new LinkedList<>();
        //2.判断根节点，若为空直接返回
        if(root==null){
            return ;
        }
        //3.将根节点放入队列中
        queue.offer(root);
        while(!queue.isEmpty()){//队列为空时，遍历结束
            //4.用cur记录下根节点，用于寻找root左右节点
            TreeNode cur=queue.poll();
            //5.打印根节点的值
            System.out.print(cur.val+" ");
            //6.判断cur的左节点是否为空，若不为空，将其存入队列
            if(cur.left!=null){
                queue.offer(root.left);
            }
            //7.右节点同理
            if(cur.right!=null){
                queue.offer(root.right);
            }
        }
        System.out.println();
    }
    /**
     * 层序遍历，用二维数组表示二叉树结构
     */
    public List<List<Integer>> levelOrder2(TreeNode root) {
        //创建二位数组，每一个元素代表每一层
        List<List<Integer>> ret=new ArrayList<>();
        //若是空树，直接返回ret,使数组为空
        if(root==null){
            return ret;
        }
        //创建一个队列，并放入根节点
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            //用size记录每一层节点的个数
            int size=queue.size();
            //创建一个一维数组，用于存放每一层的节点的值
            List<Integer> list=new ArrayList<>();
            while(size>0){
                //用cur记录从队列弹出的节点，并放入list数组中
                TreeNode cur=queue.poll();
//                list.add(cur.val);//这里会报错是因为题目里的int,而节点值的类型是char
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
                size--;
            }
            //将list数组存放到ret二维数组中
            ret.add(list);
        }
        return ret;
    }

    //完全二叉树判断
    public boolean pd(TreeNode root){
        if(root==null){
            return true;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            //这里不同
            if(cur==null){
                return false;
            }
            //
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        return true;
    }
    //完全二叉树判断  博哥写的
    public boolean isCompleteTree(TreeNode root){
        if(root==null){
            return true;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur==null){
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while (!queue.isEmpty()) {
            TreeNode node =queue.peek();
            if(node==null){
                return false;
            }else{
                queue.poll();
            }
        }
        return true;
    }

    //寻找公共祖先
    public TreeNode lowestCommonAncestor
    (TreeNode root, TreeNode p, TreeNode q) {
        if(root==null){
            return null;
        }
        if(p==root||q==root){
            return root;
        }
        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);
        if(leftTree!=null&&rightTree!=null){
            return root;
        }else if(leftTree!=null){
            return leftTree;
        }else{
            return rightTree;
        }
    }
    //法二
    public TreeNode lowestCommonAncestor2
            (TreeNode root, TreeNode p, TreeNode q) {
        if(root==null){
            return root;
        }
        //1.获取指定路径下的节点
        Stack<TreeNode> stackP=new Stack<>();
        getPath(root,p,stackP);
        Stack<TreeNode> stackQ=new Stack<>();
        getPath(root,q,stackQ);

        int sizeP=stackP.size();
        int sizeQ=stackQ.size();
        if(sizeP>sizeQ){
            int size=sizeP-sizeQ;
            while(size!=0){
                stackP.pop();
                size--;
            }
        }else{
            int size=sizeQ-sizeP;
            while(size!=0){
                stackQ.pop();
                size--;
            }
        }
        //此时stackP和Q一样长
        while(!stackP.isEmpty()&&!stackQ.isEmpty()){
            if (stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            }else{
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }
    //将根节点到指定节点全部存放在栈中
    public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if(root==null){
            return false;
        }
        stack.push(root);
        if(root==node){
            return true;
        }
        boolean flg=getPath(root.left,node,stack);
        if(flg){
            return true;
        }
        boolean flg2=getPath(root.right,node,stack);
        if(flg2){
            return true;
        }
        stack.pop();
        return false;
    }

    //非递归实现前序遍历
    public void preOrderNot(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            cur=top.right;
        }
    }

    //非递归实现中序遍历
    public void inOrderNot(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }
    }
}











